<?php

/**
 * @author PeoneEr
 * @desc соединения с БД
 * @package aion personal cabinet v1.0
 * @copyright Aion Factory Inc. 2010-2011
 * @license GNU v3
 * @link Ai-Factory.ru && http://code.google.com/p/aionprivateofficev2
 */
require_once "config.php";

class DB extends Config {

    private $_connectionToGS;
    private $_connectionToLS;
    private static $_instance = NULL;

    public function openConnectionToGS() {
        /**
         * @desc коннект к гейм базе
         */
        $this->_connectionToGS = mysql_connect($this->configData['db_host'], $this->configData['db_user'], $this->configData['db_pass']);
        if (!$this->_connectionToGS) {
            die("<div class='errorNotifi'>Database connection failed: " . mysql_error() . "</div>");
        } else {
            $db_select_game = mysql_select_db($this->configData['db_name_game']);
            if (!$db_select_game) {
                die("<div class='errorNotifi'>Database selection failed: " . mysql_error() . "</div>");
            }
        }
        mysql_query("set names utf8") or die("set names utf8 failed");
    }

    public function openConnectionToLS() {
        /**
         * @desc коннект к логин базе
         */
        $this->_connectionToLS = mysql_connect($this->configData['db_host'], $this->configData['db_user'], $this->configData['db_pass']);
        if (!$this->_connectionToLS) {
            die("Соединение с базой данных не установлено: " . mysql_error());
        } else {
            $db_select_login = mysql_select_db($this->configData['db_name_login']);
            if (!$db_select_login) {
                die("Выбор БД не удачен: " . mysql_error());
            }
        }
        mysql_query("set names utf8") or die("set names utf8 failed");
    }

    public function sql($query) {
        $result = mysql_query($query);
        if (!$result) {
            die("Database query failed: " . mysql_error());
        }
        return $result;
    }

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    public function closeConnection() {
        /**
         * @desc закрытие коннекта
         */
        try {
            mysql_close();
        } catch (Exception $e) {
            echo $e->getMessage();
            echo mysql_error();
        }
    }

}

?>
